package com.itheima.controller;

import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService1;
    @PostMapping("/login")
    public Result login(@RequestBody Emp emp2){//要改造成登录成功即下发jwt令牌的
        log.info("登录用户:{},",emp2);
        Emp e=empService1.login(emp2);//这里返回一个员工对象
        //登陆成功
        if(e!=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            claims.put("name",e.getName());
            String jwt= JwtUtils.generateJwt(claims);//生成jwt令牌
            return Result.success(jwt);
        }
        return  Result.error();
    }
}
